import { Component } from '@angular/core';

import { NavController, NavParams, ToastController } from 'ionic-angular';
import { LoginDataProvider } from '../../providers/login-data/login-data';
import { User } from '../../providers/login-data/user';
import { Storage } from '@ionic/storage';
import { TabsPage } from '../tabs/tabs';
import {PersonDetailPage} from '../person-detail/person-detail'
import { UserSettingProvider } from '../../providers/user-setting/user-setting';


@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  user: User;


  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams,
    public toastCtrl: ToastController,
    public loginDataProvider:LoginDataProvider,
    public userSettingProvider: UserSettingProvider,

    public storage: Storage) {
      
  }
  
  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }

  /**
   * 登录
   * @param username 
   * @param passWord 
   * @param remember 
   */
  login(username:HTMLInputElement,passWord:HTMLInputElement,remember:HTMLInputElement){
    let _username = username.value.trim();
    let _password = passWord.value.trim();
    let _remember = remember.value;
    // console.log(_username);
    // console.log(_password);

    
    if (_username.length === 0) {
      this.showToast('bottom','请输入账号');
      return;
    }
    if (_password.length === 0) {
      this.showToast('bottom','请输入密码');
      return;
    }

    // this.navCtrl.push(PersonDetailPage,{username:});
    this.loginDataProvider.login(_username,_password)
      .toPromise()
      .then(res=>{
        console.log('登录成功');
        // console.log(res);
        this.user=res;
        
        return true;
      },e=>{
        console.log('登录失败');
        console.log(e);
        return false;
      }).then(res=>{
        if (res) {
          let data = {
            username:this.user.username,
            password:this.user.password,
            objectId:this.user.objectId,
            sessionToKen:this.user.sessionToken,
            name: this.user.name,
            leval:this.user.leval,
            mobilePhoneNumber:this.user.mobilePhoneNumber,
            sex:this.user.sex,
            belong:this.user.belong,
            position:this.user.position,
            remember:_remember
          };
          // 保存“记住”的用户信息
          if (_remember) {
            this.storage.remove('USER_INFO');
            this.storage.set('USER_INFO',JSON.stringify(data));

          
          }
          // 保存“登录”的用户信息
          this.storage.set('LOGIN_INFO',JSON.stringify(data));
          this.navCtrl.setRoot(TabsPage);
          localStorage.setItem("objectId",data.objectId);
          localStorage.setItem("username",data.username);
          localStorage.setItem("name",data.name);
          localStorage.setItem("mobilePhoneNumber",data.mobilePhoneNumber);
          localStorage.setItem("leval",data.leval);
          localStorage.setItem("sex",data.sex);
          localStorage.setItem("belong",data.belong);
          localStorage.setItem("position",data.position);


          
        //  console.log(this.navCtrl.push(PersonDetailPage, { item: data}));
          
        }else{
          this.showToast('bottom','用户名或密码错误');
        }
        // this.userSettingProvider.getUserDetail(this.user.objectId).subscribe(user=>{
        //   this.navCtrl.push(PersonDetailPage,{user:user});
        // })

      });
      
  }

  /**
   * 注册
   * @param username 
   * @param passWord 
   * @param remember 
   */
  register(username:HTMLInputElement,passWord:HTMLInputElement,remember:HTMLInputElement){

    let _username = username.value.trim();
    let _password = passWord.value.trim();
    if (_username.length === 0) {
      this.showToast('bottom','账号不能为空');
      return;
    }
    if (_password.length === 0) {
      this.showToast('bottom','密码不能为空');
      return;
    }
    this.loginDataProvider.register(_username,_password)
    .toPromise()
      .then(res=>{
        console.log('注册成功');
        console.log(res);
        this.user=res;
        return true;
      },e=>{
        console.log('注册失败');
        console.log(e);
        return false;
      }).then(res=>{
        if (res) {
          this.showToast('bottom','注册成功');
          this.navCtrl.setRoot(this.navCtrl.getActive().component);
        }else{
          this.showToast('bottom','账号已存在！');
        }
      }
      );
  }
  /**
   * 显示提示信息
   * @param position 
   * @param message 
   */
  showToast(position:string,message:string){
    let toast = this.toastCtrl.create({
      message:message,
      duration:2000,
      position:position
    });
    toast.present();
  }
  



}
